forval x=1/4 {
	use "./lil-0246b/Stata/indiv03`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind3`x'
	save `ind3`x'', replace
}

forval x=1/4 {
	use "./lil-0247b/Stata/indiv04`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind4`x'
	save `ind4`x'', replace
}

forval x=1/4 {
	use "./lil-0317b/Stata/indiv05`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind5`x'
	save `ind5`x'', replace
}

forval x=1/4 {
	use "./lil-0390b/Stata/indiv06`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind6`x'
	save `ind6`x'', replace
}

forval x=1/4 {
	use "./lil-0399b/Stata/indiv07`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind7`x'
	save `ind7`x'', replace
}

forval x=1/4 {
	use "./lil-0476b/Stata/indiv08`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind8`x'
	save `ind8`x'', replace
}

forval x=1/4 {
	use "./lil-0541b/Stata/indiv09`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind9`x'
	save `ind9`x'', replace
}

forval x=1/4 {
	use "./lil-0684b/Stata/indiv10`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind10`x'
	save `ind10`x'', replace
}

forval x=1/4 {
	use "./lil-0721b/Stata/INDIV11`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind11`x'
	save `ind11`x'', replace
}

forval x=1/4 {
	use "./lil-0825b/Stata/INDIV12`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind12`x'
	save `ind12`x'', replace
}

forval x=1/4 {
	use "./lil-0944b/Stata/INDIV13`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind13`x'
	save `ind13`x'', replace
}

forval x=1/4 {
	use "./lil-1000b/Stata/INDIV14`x'", clear
	destring ag, replace
	destring naia, gen(birthyear)
	keep if birthyear>=1971	
	gen female=(sexe=="2")
	destring annee, gen(surveyr)
	tempfile ind14`x'
	save `ind14`x'', replace
}

use `ind31', clear
forval y=2/4 {
	append using `ind3`y''
}
forval x=4/14 {
	forval y=1/4 {
		append using `ind`x'`y'', force
	}
}

// Outcome variables
// Education
destring dip11,replace
gen secondary=(dip11<=50)
replace secondary=. if dip11==.

gen bacormore=(dip11<=42)
replace bacormore=. if dip11==.

destring forniv, replace
destring acteu6, replace
gen atschool_prebac=(acteu6==5&forniv>=2&forniv<=6&surveyr<2008)
replace atschool_prebac=(acteu6==5&forniv>=2&forniv<6&surveyr>=2008)
replace atschool_prebac=0 if forniv==.

// Long-term outcomes
destring acteu, replace
gen inactive=(acteu==3)
replace inactive=. if acteu==.

gen employed=(acteu==1)
replace employed=. if acteu==.

destring em1, replace
destring nbenfind, replace force
gen children=(em1==1)
replace children=. if em1==.
replace children=1 if nbenfind>0&surveyr>=2013
replace children=. if nbenfind==.&surveyr>=2013

destring lpr, replace
destring lprm, replace
gen child=(lpr==3)
replace child=1 if lprm==3
replace child=. if (lpr==.&surveyr<2013)|(lprm==.&surveyr>=2013)

destring matri, replace
gen married=(matri==2)
gen evermarried=(matri!=1)
replace married=. if matri==.


// other demographics+controls
destring lnais, replace

// nationality of parent (if present in household)
destring nat28pr, replace
gen parentalg=(nat28pr==11)
gen parenttun=(nat28pr==12)
gen parentmor=(nat28pr==13)
gen parentturk=(nat28pr==45)

// treated cohorts
gen treatedcoh=(birthyear>=1986)

// Declare panel
egen ind=group(ident noi)
egen time=group(annee trim)
xtset ind time

// Keep if born 1980 or later
keep if birthyear>=1980

tempfile panel
save `panel'

keep ident noi annee trim ind time birthyear parentalg parenttun parentmor parenttur nat28pr surveyr acteu6 forniv ag lnais child female spr

save panel, replace

use `panel', clear

// Apply restrictions:
// 20 years or older at time of survey
drop if ag<20

// Drop countries we cannot identify as Muslim or non-Muslim
drop if paiperc=="07"|paiperc=="10"|paiperc=="99" 

// unique identifier in terms of survey year, quarter, household #, individual #
// keep first individual occurrence (using variable RGI results in the same)
destring noi, gen(person)
egen personid=group(person ident)
sort personid annee trim
egen tag=tag(personid)
keep if tag==1
drop tag

keep noi personid paiperc paimerc birthyear surveyr secondary bacormore inactive employed child children married ///
		atschool_prebac parent* forniv pnai28 lnais female ag nat28pr acteu6 tau10 ///
		acteu6pr cstotpr spr zus treatedcoh 

save repeated_cross_section, replace


		
